package com.pe1n.yangtaopai.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ProviderInfo;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.BigCarBean;
import com.pe1n.yangtaopai.bean.CarBean;
import com.pe1n.yangtaopai.bean.CarBottomBean;
import com.pe1n.yangtaopai.helper.IupDataListener;
import com.pe1n.yangtaopai.helper.IupDataListenerTwo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by My on 2016/11/26.
 */

public class ShoppingFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;//上下文
    private List<Object> mList;//数据源
    private double aDouble;
    private  final int TYPE_ONE=1;
    private final int TYPE_TWO=2;
    private SharedPreferences sharedPreferences;
    private IupDataListenerTwo iupDataListenerTwo;
    private boolean boo;
    public ShoppingFragmentAdapter(Context mContext, List<Object> mList,IupDataListenerTwo iupDataListenerTwo) {
        this.mContext = mContext;
        this.mList = mList;
        this.iupDataListenerTwo= iupDataListenerTwo;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=null;
        switch (viewType){
            case TYPE_ONE :
                view=LayoutInflater.from(mContext).inflate(R.layout.fragment_car_item_recyclerview_one,parent,false);
             return new  MyViewHolder1(view) ;
            case TYPE_TWO:
                view=LayoutInflater.from(mContext).inflate(R.layout.fragment_car_item_reclerview_two,parent,false);
                return new  MyViewHolder2(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
      if(holder instanceof MyViewHolder1){
          BigCarBean carBean= ( BigCarBean) mList.get(position);
          sharedPreferences=mContext.getSharedPreferences("myshared",0);
           boo=sharedPreferences.getBoolean("isRegister",false);
          if( boo){
              ((MyViewHolder1) holder).mTextViewLogin.setText("请添加购物车");
              ((MyViewHolder1) holder).mTextViewLogin.setVisibility(View.GONE);
              ((MyViewHolder1) holder).mLinearLayout.setVisibility(View.VISIBLE);
          }
          ((MyViewHolder1) holder).resetData( carBean.getList());
      }
        else if(holder instanceof MyViewHolder2){
          List<CarBottomBean.DataData.ProdListsData> newList=((CarBottomBean)mList.get(position)).getData().getProdLists();
          ((MyViewHolder2) holder).resetData2( newList);
      }
    }

    @Override
    public int getItemCount() {
        return mList.size();

    }

    @Override
    public int getItemViewType(int position) {
        if(mList.get(position) instanceof BigCarBean){
            return TYPE_ONE;
        }
            else if(mList.get(position) instanceof CarBottomBean){
                return TYPE_TWO;
            }

        return super.getItemViewType(position);
    }

    class MyViewHolder1 extends RecyclerView.ViewHolder{
         IupDataListener iupDataListener=new IupDataListener() {
             @Override
             public void updata(double price ,boolean b) {
                 //if(price==0.0){
                    // aDouble=0.0;
                // }

                 if(b){
                     aDouble+=price;
                     if(price==0.0){
                         aDouble=0.0;
                     }
                 }
                 else{
                     aDouble-=price;
                     if( aDouble<0.0){
                         aDouble=0.0;
                     }
                 }
                 iupDataListenerTwo.upDataTwo(price,b);
                 mTextViewMoney.setText("¥"+aDouble);
             }
         };
        ImageView imageViewSelect;//选择框
        RecyclerView mRecyclerView;//滑动的控件
        TextView mTextViewMoney;//钱
        LinearLayout mLinearLayout;
        TextView mTextViewLogin;//登录
        //SharedPreferences sharedPreferences;//判断是登录了
        CarBottomReclerAdapter carBottomReclerAdapter;
        List<CarBean> mLists;
        public void resetData(List<CarBean> list){
            mLists.clear();
            mLists.addAll(list);
            carBottomReclerAdapter.notifyDataSetChanged();
        }
        public MyViewHolder1(View itemView) {
            super(itemView);
            mLists=new ArrayList<>();
            mLinearLayout= (LinearLayout) itemView.findViewById(R.id.fragment_car_item_recyclerViewO_linearLayout);
            imageViewSelect= (ImageView) itemView.findViewById(R.id.fragment_car_item_recyclerViewO_select);
            mRecyclerView= (RecyclerView) itemView.findViewById(R.id.fragment_car_item_recyclerViewO_recyclerView);
            mTextViewMoney= (TextView) itemView.findViewById(R.id.fragment_car_item_recyclerViewO_allMoney);
            mTextViewLogin= (TextView) itemView.findViewById(R.id.fragment_car_item_recyclerViewO_login);
            carBottomReclerAdapter=new CarBottomReclerAdapter(mContext,mLists,iupDataListener,iupDataListenerTwo);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            mRecyclerView.setAdapter(carBottomReclerAdapter);
            sharedPreferences=mContext.getSharedPreferences("myshared",0);
             boo=sharedPreferences.getBoolean("isRegister",false);
            if ( boo){
                mTextViewLogin.setVisibility(View.GONE);
                mLinearLayout.setVisibility(View.VISIBLE);
            }
            if(!boo){
                mLinearLayout.setVisibility(View.INVISIBLE);
            }

        }
    }
    class MyViewHolder2 extends RecyclerView.ViewHolder{
          LinearLayout mLinearLayoutInfo;
          RecyclerView mRecyclerView;//滑动控件
          CarTopRecyclerAdapter carTopRecyclerAdapter;
          List<CarBottomBean.DataData.ProdListsData> mLsitCarBottom;
        public void resetData2(List<CarBottomBean.DataData.ProdListsData> mList){
            mLsitCarBottom.addAll( mList);
            carTopRecyclerAdapter.notifyDataSetChanged();
        }
        public MyViewHolder2(View itemView) {
            super(itemView);
            mLsitCarBottom=new ArrayList<>();
            carTopRecyclerAdapter=new CarTopRecyclerAdapter(mContext,mLsitCarBottom);
            mLinearLayoutInfo= (LinearLayout) itemView.findViewById(R.id.fragment_car_item_recyclerViewT_moreInfo);
            mRecyclerView= (RecyclerView) itemView.findViewById(R.id.fragment_car_item_recyclerViewT_recyclerView);
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,3));
            mRecyclerView.setAdapter(carTopRecyclerAdapter);

        }
    }

}
